package cn.tedu.pojo;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.io.InputStream;
import java.util.List;

public class MybatisDemo02 {
    public static void main(String[] args)  throws Exception {
        //1.读取mybatis核心配置文件中的配置信息(mybatis-config.xml)
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        //2.基于上面读取的配置信息获取SqlSessionFactory对象(工厂)
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.打开与数据库的连接(即通过工厂对象获取SqlSession对象)
        SqlSession session = fac.openSession();
        //4.通过namespace+id找到并执行SQL语句, 返回处理后的结果
        //EmpMapper.xml, List<Emp>
        List<Emp> list = session.selectList("EmpMapper.findAll");
        //5.输出结果
        for (Emp emp : list) {
            System.out.println(emp);
        }

    }

}
